package com.ynny.dao;

import com.ynny.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public User selectUserById(Integer userId) {

        String sql = "select * from t_user where user_id=?";
        User user = jdbcTemplate.queryForObject(sql,
                new BeanPropertyRowMapper<>(User.class), userId);
        return user;
    }

    @Override
    public int updateBalance(Integer userId, Integer price) {

        String sql = "update t_user set balance=balance - ? where user_id=?";
        int i = jdbcTemplate.update(sql, price, userId);
        return i;
    }
}
